package com.wframe.project.former.web.floor;

import com.wframe.project.jnd.entity.UiFloorItem;
import com.wframe.project.jnd.entity.UiFloorProduct;
import com.wframe.project.jnd.rest.util.JsonReturn;
import com.wframe.project.jnd.services.UiFloorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lwb on 2017/5/25.
 */
@Controller
@RequestMapping("/floor")
public class FloorController {
    @Autowired
    private UiFloorService floorService;

    /**
     * 跳转添加链图数据窗口
     *
     * @return
     */
    @RequestMapping(value = "/item/{panid}", method = RequestMethod.GET)
    public String showItem(@PathVariable String panid, Model model) {
        model.addAttribute("panid", panid);
        return "/webadmin/floor/floorItemsEdit";
    }

    /**
     * 跳转编辑链图数据窗口
     *
     * @return
     */
    @RequestMapping(value = "/item/{panid}/{id}", method = RequestMethod.GET)
    public String showItem(@PathVariable("panid") String panid, @PathVariable("id") String id, Model model) {
        model.addAttribute("panid", panid);
        model.addAttribute(floorService.findOne(id));
        return "/webadmin/floor/floorItemsEdit";
    }

    /**
     * 显示全部链图数据
     *
     * @return
     */
    @RequestMapping(value = "/items/{id}", method = RequestMethod.GET)
    public String showItems(@PathVariable Long id, Model model) {
        List<UiFloorItem> items = floorService.findAll(id);
        model.addAttribute("items", items);
        model.addAttribute("panid", id);
        return "/webadmin/floor/floorItems";
    }

    /**
     * 添加或者修改链图数据
     *
     * @param item
     * @return
     */
    @RequestMapping(value = "/item", method = RequestMethod.POST)
    @ResponseBody
    public String saveOrUpdateItem(UiFloorItem item) {
        JsonReturn json = new JsonReturn();
        item.setCreate_time(new Date());
        floorService.saveOrUpdate(item);
        return json.toString();
    }

    /**
     * 删除链图数据
     *
     * @param ids
     * @param map
     * @return
     */
    @RequestMapping(value = "/item", method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteItems(@RequestParam("ids[]") String[] ids, ModelMap map) {
        JsonReturn json = new JsonReturn();
        if (ids != null && ids.length > 0) {
            map.put("id", ids);
            floorService.deleteItems(map);
        }
        return json.toString();
    }

    /**
     * 添加商品数据
     *
     * @return
     */
    @RequestMapping(value = "/product/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String addProducts(@PathVariable("id") Long id, @RequestParam("ids[]") Long[] ids, ModelMap map) {
        JsonReturn json = new JsonReturn();
        List<UiFloorProduct> products = new ArrayList<>();
        if (ids != null && ids.length > 0) {
            for (int i = 0; i < ids.length; i++) {
                UiFloorProduct product = new UiFloorProduct();
                product.setUi_floor_panel_id(id);
                product.setWb_product_id(ids[i]);
                product.setSort_value(i);
            }

        }
        map.put("id", id);
        floorService.addProducts(products);
        return json.toString();
    }


    /**
     * 删除商品数据
     *
     * @param ids
     * @param map
     * @return
     */
    @RequestMapping(value = "/product", method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteProducts(@RequestParam("ids[]") String[] ids, ModelMap map) {
        JsonReturn json = new JsonReturn();
        if (ids != null && ids.length > 0) {
            map.put("id", ids);
            floorService.deleteProducts(map);
        }
        return json.toString();
    }

}
